﻿using System;
using System.Linq;
using System.Text;

namespace ControleRepresentante.Logic.Models
{
    public class UserInfo
    {
        private readonly PermissaoInfo[] m_Permissoes;

        public UserInfo(User user)
        {
            Id = user.Id;
            Username = user.Username;
            IsAdmin = user.IsAdmin;
            Ativo = user.Ativo;
            m_Permissoes = user.Permissoes.Select(x => new PermissaoInfo(x.Id, x.Area, x.Operacao)).ToArray();
        }

        public bool Ativo { get; private set; }
        public bool IsAdmin { get; private set; }
        public string Username { get; private set; }
        public Guid Id { get; private set; }

        public bool HasPermission(Area area, Operacao operacao)
        {
            return User.HasPermission(area, operacao, m_Permissoes.ToDictionary(x => x.Area, x => x.Operacao));
        }

        public override string ToString()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine("Id: " + Id.ToString());
            stringBuilder.AppendLine("IsAdmin: " + IsAdmin.ToString());
            stringBuilder.AppendLine("Username: " + Username);
            foreach (PermissaoInfo permissaoInfo in m_Permissoes)
            {
                stringBuilder.AppendLine("\tId: " + permissaoInfo.Id);
                stringBuilder.AppendLine("\tArea: " + Enum.GetName(typeof (Area), permissaoInfo.Area));
                stringBuilder.AppendLine("\tOperacao: " + permissaoInfo.Operacao);
            }
            return stringBuilder.ToString();
        }
    }
}
